package com.lagou.homework.studentmanagerpro;

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * @author Rentaian
 */
public class ManagerClass {
    private List<Student> listStudent = new LinkedList<>();

    /**
     * 写一个方法来反复打印界面表示开始
     */
    public void startSys() {
        System.out.println("########################################################################");
        System.out.println("#########################欢迎来到学生信息管理系统#########################");
        System.out.println("####################请输入命令选择学生管理系统的功能：#####################");
        System.out.println("#########1:增加   2:删除    3:修改    4:查找    5:遍历    0:结束##########");
        System.out.println("########################################################################");
        System.out.println("##############################请输入命令:################################");
    }

    /**
     * 定义一个方法加载用于开始系统加载学生信息
     */
    public void studentMessageLoader() {
        ObjectInputStream ois = null;
        try {
            //创建一个file对象用于判断文件是否存在
            File file = new File("d:\\studentMsg.txt");
            //判断文件是否存在，若存在则读出
            if (file.exists()) {
                ois = new ObjectInputStream(new FileInputStream(file));
                Object o = ois.readObject();
                listStudent = (List<Student>)o;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 定义一个方法用于读取用户的输入来调用程序的功能
     */
    public void manager() {
        studentMessageLoader();
        Scanner sc = new Scanner(System.in);
        outer:
        while (true) {
            startSys();
            int orderNum = sc.nextInt();
            switch (orderNum) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    selectStudentById();
                    break;
                case 5:
                    selectAllStudent();
                    break;
                case 0:
                    finishSys();
                    break outer;
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }
    }

    /**
     * 此方法根据学号查找学生信息
     */
    private void selectStudentById() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的学生信息的id:");
        boolean flag = false;
        int id = sc.nextInt();
        for (Student student : listStudent) {
            if (id == student.getId()) {
                flag = true;
                System.out.println("id为" + student.getId() + "的学生信息是： " + student.toString());
            }
        }
        if(!flag) {
            System.out.println("没有查找到该学生!");
        }
    }

    /**
     * 查找所有学生信息的方法
     */
    private void selectAllStudent() {
        for (Student student : listStudent) {
            System.out.println(student.getName() + " 同学的id为: " + student.getId() + " 性别为: " + student.getGender() + " 年龄为: " + student.getAge());
        }
    }

    private void updateStudent() {
        System.out.println("请输入要修改的学生的信息的学号: ");
        boolean flag = false;
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        for (Student stu : listStudent) {
            if (id == stu.getId()) {
                flag = true;
                try {
                    System.out.println("输入修改后的id:");
                    int i = sc.nextInt();
                    stu.setId(i);
                    System.out.println("请输入要修改的学生的姓名: ");
                    String name = sc.next();
                    stu.setName(name);
                    System.out.println("请输入要修改学生的年龄: ");
                    int age = sc.nextInt();
                    stu.setAge(age);
                    System.out.println("请输入要修改学生的性别：");
                    String gender = sc.next();
                    stu.setGender(gender);
                } catch (IdException e) {
                    e.printStackTrace();
                } catch (AgeException e) {
                    e.printStackTrace();
                }
            }
        }
        if(!flag) {
            System.out.println("您输入的id有误！");
        }
    }

    /**
     * 根据学号删除学生信息，然后打印剩下的学生信息
     */
    private void deleteStudent() {
        System.out.println("请输入要删除的学生信息的学号: ");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        for (Student stu : listStudent) {
            if (id == stu.getId()) {
                listStudent.remove(stu);
            }
        }
        System.out.println("删除后的学生信息是：");
        for (Student student : listStudent) {
            System.out.println(student.getName() + " 同学的id为: " + student.getId() + " 性别为: " + student.getGender() + " 年龄为: " + student.getAge());
        }
    }

    /**
     * 添加学生信息
     */
    private void addStudent() {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要添加的学生的信息的id:");
        int id = sc.nextInt();
        System.out.println("请输入要添加的学生的信息的姓名:");
        String name = sc.next();
        System.out.println("请输入要添加的学生的信息的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入要添加的学生的信息的性别:");
        String gender = sc.next();
        Student stu = null;

        try {
            stu = new Student(id, name, age, gender);
        } catch (IdException e) {
            e.printStackTrace();
        } catch (AgeException e) {
            e.printStackTrace();
        }

        listStudent.add(stu);
        System.out.println("添加后的学生信息是：");
        for (Student student : listStudent) {
            System.out.println(student.getName() + " 同学的id为: " + student.getId() + " 性别为: " + student.getGender() + " 年龄为: " + student.getAge());
        }
    }

    /**
     * 结束系统将学生信息写入到文件中
     * 为避免重复写入读入学生信息，若文件存在则删除,然后重新写入信息
     *
     * @return
     */
    public void finishSys() {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        try {
            //创建一个file对象用于判断文件是否存在
            File file = new File("d:\\studentMsg.txt");
            oos = new ObjectOutputStream(new FileOutputStream(file));
            //判断文件是否存在，若存在则删除,然后写入,若不存在,创建并写入
            if (file.exists()) {
                file.delete();
                oos.writeObject(listStudent);
            } else {
                oos.writeObject(listStudent);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
